package com.demo.cms.webapp.template.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.demo.cms.model.Category;
import com.demo.cms.service.CategoryService;
import com.demo.common.StringUtils;
import com.demo.webapp.views.freemarker.directive.core.PageContextDirectiveSupport;

import freemarker.core.Environment;
import freemarker.core.Environment.TemplateDirectiveBodyEx;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * 栏目的自定义指令
 * 
 * @author ryuu.kk
 *
 */
public class CategoryDirective extends PageContextDirectiveSupport {

	/**
	 * 页面模板名
	 */
	private static final String CATEGORY_TEMPLATE_NAME = "classpath:/com/demo/cms/webapp/template/category.ftl";
	
	/**
	 * 栏目服务
	 */
	@Autowired
	private CategoryService categoryService;
	
	@SuppressWarnings("rawtypes")
	@Override
	public void preWriter(Environment env, TemplateDirectiveBodyEx bodyEx,
			TemplateModel[] loopVars, SimpleHash hash, Map params)
			throws IOException, TemplateException {
		// 栏目id
		Long id = null;
		// 栏目路径
		String categoryPath = "";
		// 是否取得栏目树
		boolean tree = false;
		// 是否取得根节点
		boolean root = false;
		// 反向追溯
		boolean reverse = false;
		// 取得树的层级
		int level = 0;
		
		Category category = null;
		id = getLong("id", params);
		// 栏目路径
		categoryPath = getString("categoryPath", params);
		level = getInteger("level", params, level);
		int[] depaths = null;
		if (level > 0) {
			depaths = new int[level];
			for (int i = 0; i < level; i ++) {
				depaths[i] = i;
			}
		}
		tree = getBoolean("tree", params, Boolean.valueOf(getDefaultValue("tree")));
		root = getBoolean("root", params, Boolean.valueOf(getDefaultValue("root")));
		reverse = getBoolean("reverse", params, Boolean.valueOf(getDefaultValue("reverse")));
		
		// 使用id查询栏目优先
		if (id != null) {
			if (tree) {
				// 树形结构的栏目
				category = (Category) categoryService.queryTree(id, depaths);
			} else {
				// 一般栏目
				category = categoryService.find(id);
			}
		} else {
			if (root) {
				// 查询根
				category = categoryService.findRoot();
				if (tree) {
					if (category != null) {
						category = (Category) categoryService.queryTree(category.getId(), depaths);
					}
				}
			} else {
				if (StringUtils.isNotEmpty(categoryPath)) {
					if (tree) {
						if (reverse) {
							category = (Category) categoryService.queryTree(categoryPath, Boolean.TRUE, depaths);
						} else {
							category = (Category) categoryService.queryTree(categoryPath, depaths);
						}
					} else {
						// 查询根
						category = categoryService.find(categoryPath);
					}
				}
			}
		}
		hash.put("channelPath", category.getChannelPath());
		hash.put("categoryPath", category.getPath());
		hash.put("category", category);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String getTemplatePath(Map params) {
		String path = "";
		try {
			path = getString("path", params, CATEGORY_TEMPLATE_NAME);
		} catch (TemplateModelException e) {}
		return path;
	}
	
	@Override
	public void afterInvoke(Writer writer) {
	}
}
